const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    imgPath: [],
    picker: ['证件', '书籍', '数码产品', '生活用品', '其他'],
    title: '',
    caption: '',
    postType: '',
    itemType: '',
    isOnTop: 1,
    switch1Checked: false,
    userinfo: []
  },
  PickerChange(e) {
    this.setData({
      index: e.detail.value,
      itemType: e.detail.value
    })
  },
  ChooseImage() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  DelImg(e) {
    var imgs = this.data.imgList;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgList: imgs
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  title(e) {
    this.data.title = e.detail.value
  },
  onChange(e) {
    const {
      currentKey
    } = {
      ...e.detail
    };
    this.setData({
      currentKey
    });
  },
  switch1Change(e) {
    // 确定置顶
    if (!this.data.switch1Checked) {
      var that = this
      wx.showModal({
        title: '提示',
        content: '是否花费100积分使用置顶功能？',
        success: function (res) {
          if (res.confirm) {
            that.setData({
              switch1Checked: e.detail.value,
              isOnTop: 0
            })
          } else {
            that.setData({
              switch1Checked: false,
              isOnTop: 1
            })
          }
        }
      })
    } else { //不置顶
      this.setData({
        switch1Checked: false,
        isOnTop: 1
      })
    }
  },
  textareaBInput(e) {
    this.setData({
      caption: e.detail.value
    })
  },
  commit() {
    if (this.data.title == '' || this.data.currentKey == '' || this.data.index == '') {
      wx.showToast({
        title: '请填写完整！',
        icon: 'none'
      })
    } else {
      if (wx.getStorageSync('userinfo').province == '' || wx.getStorageSync('userinfo').city == '' || wx.getStorageSync('userinfo').address == '' || wx.getStorageSync('userinfo').school == '') {
        wx.showModal({
          title: '提示',
          content: '请先去"我的"页面填写完整区域信息以方便您的帖子更容易被附近的人发现哦~',
          success: function (res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../mine/mine',
              })
            } else {

            }
          }
        })
      } else {
        var that = this;
        this.setData({
          imgPath: []
        })
        var imgCopy = []
        if (this.data.imgList.length != 0) {
          for (var i = 0; i < that.data.imgList.length; i++) {
            new Promise(async (resolve, reject) => {
              const fileSystemManager = wx.getFileSystemManager()
              fileSystemManager.readFile({
                filePath: that.data.imgList[i], // 例如图片临时路径
                encoding: 'base64',
                success(res1) {
                  let {
                    data
                  } = res1 // 编码后的数据
                  that.data.imgPath.push(data)
                  imgCopy = that.data.imgPath
                  resolve(imgCopy)
                }
              })
            }).then(res => {
              if (res.length == i) {
                wx.User.addNewPost({
                  data: {
                    title: that.data.title,
                    caption: that.data.caption,
                    postType: that.data.currentKey,
                    itemType: that.data.index,
                    isOnTop: that.data.isOnTop,
                    uid: wx.getStorageSync('userinfo').uid,
                    imgList: res
                  }
                })
                that.setData({
                  caption: '',
                  imgList: [],
                  imgPath: [],
                  isOnTop: 1,
                  postType: '',
                  switch1Checked: false,
                  title: '',
                  index: ''
                })
              }
            })
          }
        } else {
          wx.User.addNewPost({
            data: {
              title: that.data.title,
              caption: that.data.caption,
              postType: that.data.currentKey,
              itemType: that.data.index,
              isOnTop: that.data.isOnTop,
              uid: wx.getStorageSync('userinfo').uid,
              imgList: []
            }
          })
          that.setData({
            caption: '',
            imgList: [],
            imgPath: [],
            isOnTop: 1,
            postType: '',
            switch1Checked: false,
            title: '',
            index: ''
          })
        }
      }


    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.editTabbar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.editTabbar();
    app.receiverUnRead();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})